package cn.lyjuan.android.demofive.chapter12.app_12_2_5_1;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.widget.ImageView;
import cn.lyjuan.android.demofive.R;

/**
 * Created by chad on 16-1-9.
 */
public class GSensitiveView extends ImageView
{
    private Bitmap image;
    private double rotation;
    private Paint paint;

    public GSensitiveView(Context context)
    {
        super(context);

        BitmapDrawable drawable = (BitmapDrawable) context.getResources().getDrawable(R.drawable.tumbler);

        image = drawable.getBitmap();

        paint = new Paint();
    }

    @Override
    protected void onDraw(Canvas canvas)
    {
//        super.onDraw(canvas);

        double w = image.getWidth();
        double h = image.getHeight();

        Rect rect = new Rect();
        this.getDrawingRect(rect);

        int degrees = (int) (180 * rotation / Math.PI);

        canvas.rotate(degrees, rect.width() / 2, rect.height() / 2);
        canvas.drawBitmap(image, (float) ((rect.width() - w) / 2),
                (float) ((rect.height() - h ) / 2),
                paint);
    }

    public void setRotation(double rotation)
    {
        this.rotation = rotation;
        invalidate();
    }
}
